Arduino Adventures: Escape From Gemini Station by James Floyd Kelly & Harold Timmis

Arduino Adventures: Escape From Gemini Station by James Floyd Kelly & Harold Timmis

Author:James Floyd Kelly & Harold Timmis [Kelly, James Floyd & Timmis, Harold]
Language: eng
Format: epub
Tags: Computers, General, Hardware, Technology & Engineering, Robotics
ISBN: 9781430246053
Publisher: Apress
Published: 2013-01-30T05:00:00+00:00


The Challenge 4 Sketch

Let’s start with a breakdown of what we need to do in this sketch. By making a list we can be certain we don’t miss anything, and it’ll help when we begin to explore the different sections of the sketch. Here’s what has to happen in the sketch:

We have to determine what variables are needed to communicate with all of the different IO (inputs and outputs).

We’ll use the setup structure to initialize the outputs, and use a few of the Arduino’s pull-up resistors.

In the loop structure, we’ll read in the state of the push buttons.

We also need to read the state of the analog input (potentiometer).

We have to scale the potentiometer values to fall between 0 and 256. The potentiometer will give a value between 0 and 1023, but we’ll divide by 4 to reduce that range to a more easily usable one. A smaller analog range means you will have the full range of the potentiometer rather than ¼ of the potentiometers range.

Next, we have to set up a conditional statement to compare the states of each of the push buttons to ON or OFF (0 or 1). Remember, because we chose to use the Arduino’s internal pull-up resistors (we did this in Challenge 2) and because of the way we designed the circuit, the states for ON and OFF are backward; that is, 1 represents OFF and 0 represents ON. Sometimes design choices will cause you to code sketches in different ways.

Finally, we need a state that controls the condition when neither push buttons is pressed. We don’t need to worry about both buttons being pressed as the motor will spin clockwise in this case due to the order of the If-Else statement.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.